﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JXC.Application.Dtos;
using JXC.Core.Repository;
using AutoMapper;
using JXC.Core.Models;
using JXC.EFCore;
using Microsoft.EntityFrameworkCore;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JXC.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CkAdminController : ControllerBase//库存管理
    {
             
        private readonly AppDbContext _context;

        public CkAdminController(AppDbContext context)
        {
            _context = context;     
        }
        [Route("cd")]
        [HttpPost]
        public IEnumerable<Cinfo> GetAll([FromBody] TestDto testDto)
        {

            return _context.Cinfo.Where(p=>p.WarehouseName.Contains(testDto.deptName)). ToList();
        }

        /// <summary>
        /// 根据Id查找数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<Cinfo> GetById(Guid id)
        {
            var item = await _context.Cinfo.FirstOrDefaultAsync(t => t.Id == id);
            return item;
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody]Cinfo cinfo)
        {
            if (cinfo == null)
            {
                return BadRequest();
            }
            else
            {
                _context.Cinfo.Add(cinfo);
                await _context.SaveChangesAsync();
                return Ok(cinfo);
            }
        }   
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cinfo"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(Guid id, [FromBody]Cinfo cinfo)
        {
            if (cinfo == null)
            {
                return BadRequest();
            }
            else
            {
                var cinfos = _context.Cinfo.SingleOrDefault(t => t.Id == id);
                if (cinfos == null)
                {
                    return NotFound();
                }
                else
                {
                    cinfos.WarehouseName = cinfo.WarehouseName;
                    cinfos.InventoryValue = cinfo.InventoryValue;
                    _context.Cinfo.Update(cinfos);
                    await _context.SaveChangesAsync();
                    return Ok(cinfos);
                }
            }
        }

        // DELETE api/<CkAdminController>/5
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var cinfos = _context.Cinfo.SingleOrDefault(t => t.Id == id);
            if (cinfos == null)
            {
                return NotFound();
            }
            else
            {
                _context.Cinfo.Remove(cinfos);
                await _context.SaveChangesAsync();
                return Ok();
            }
        }
    }
}
